﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace RMSM
{
    [Serializable()]
    public class KBDieuDuongReduceBO : Csla.BusinessBase<KBDieuDuongReduceBO>
    {
        #region Business Properties

        private int? _KBDieuDuongID = null;
        public int? KBDieuDuongID
        {
            get { return this._KBDieuDuongID; }
            set { this._KBDieuDuongID = value; }
        }

        private int? _BenhNhanID = null;
        public int? BenhNhanID
        {
            get { return this._BenhNhanID; }
            set { this._BenhNhanID = value; }
        }

        private DateTime? _Ngay = null;
        public DateTime? Ngay
        {
            get { return this._Ngay; }
            set { this._Ngay = value; }
        }

        private int? _DieuDuongID = null;
        public int? DieuDuongID
        {
            get { return this._DieuDuongID; }
            set { this._DieuDuongID = value; }
        }

        private string _ToanThan = null;
        public string ToanThan
        {
            get { return this._ToanThan; }
            set { this._ToanThan = value; }
        }

        private string _Mach = null;
        public string Mach
        {
            get { return this._Mach; }
            set { this._Mach = value; }
        }

        private string _NhietDo = null;
        public string NhietDo
        {
            get { return this._NhietDo; }
            set { this._NhietDo = value; }
        }

        private string _HuyetAp = null;
        public string HuyetAp
        {
            get { return this._HuyetAp; }
            set { this._HuyetAp = value; }
        }

        private string _NhipTho = null;
        public string NhipTho
        {
            get { return this._NhipTho; }
            set { this._NhipTho = value; }
        }

        private string _CanNang = null;
        public string CanNang
        {
            get { return this._CanNang; }
            set { this._CanNang = value; }
        }

        private string _ChieuCao = null;
        public string ChieuCao
        {
            get { return this._ChieuCao; }
            set { this._ChieuCao = value; }
        }

        private int? _STT0 = 0;
        public int? STT0
        {
            get { return _STT0; }
            set { _STT0 = value; }
        }

        protected override object GetIdValue()
        {
            return _KBDieuDuongID.Value;
        }
        #endregion Business Properties
        //==========================================================================

        #region Factory Methods
        public void SetIsNew()
        {
            MarkNew();
        }

        public void SetIsOld()
        {
            MarkOld();
        }

        public void SetIsDirty()
        {
            MarkDirty();
        }

        public void SetIsChild()
        {
            MarkAsChild();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }

        private static int newKBDieuDuongID = -1;
        private KBDieuDuongReduceBO()
        {
            _KBDieuDuongID = newKBDieuDuongID--;
        }

        public KBDieuDuongReduceBO(SqlDataReader dataReader)
        {
            MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public KBDieuDuongReduceBO(SqlDataReader dataReader, bool isRoot)
        {
            if (!isRoot)
                MarkAsChild();
            Fetch(dataReader);
            MarkOld();
        }

        public static KBDieuDuongReduceBO NewRootKBDieuDuongReduceBO()
        {
            return DataPortal.Create<KBDieuDuongReduceBO>(new RootCriteria());
        }

        internal static KBDieuDuongReduceBO NewChildKBDieuDuongReduceBO()
        {
            return DataPortal.Create<KBDieuDuongReduceBO>(new ChildCriteria());
        }

        public static KBDieuDuongReduceBO GetRootKBDieuDuongReduceBO(int _KBDieuDuongID)
        {
            return DataPortal.Fetch<KBDieuDuongReduceBO>(new RootCriteria(_KBDieuDuongID));
        }

        public static KBDieuDuongReduceBO GetRootKBDieuDuongReduceBOByWhere(string where)
        {
            return DataPortal.Fetch<KBDieuDuongReduceBO>(where);
        }

        internal static KBDieuDuongReduceBO GetChildKBDieuDuongReduceBO(SqlDataReader dataReader)
        {
            return new KBDieuDuongReduceBO(dataReader);
        }

        public static void DeleteKBDieuDuongReduceBO(int _KBDieuDuongID)
        {
            DataPortal.Delete(new RootCriteria(_KBDieuDuongID));
        }

        public static void DeleteKBDieuDuongReduceBO(int _KBDieuDuongID, bool directDelete)
        {
            DataPortal.Delete(new RootCriteria(_KBDieuDuongID, directDelete));
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        public class RootCriteria
        {
            public int KBDieuDuongID;
            public bool directDelete = false;
            public RootCriteria(int _KBDieuDuongID)
            {
                this.KBDieuDuongID = _KBDieuDuongID;
                this.directDelete = false;
            }

            public RootCriteria(int _KBDieuDuongID, bool directDelete)
            {
                this.KBDieuDuongID = _KBDieuDuongID;
                this.directDelete = directDelete;
            }

            public RootCriteria() { }
        }

        [Serializable()]
        public class ChildCriteria
        {
            public int KBDieuDuongID;
            public bool directDelete = false;
            public ChildCriteria(int _KBDieuDuongID)
            {
                this.KBDieuDuongID = _KBDieuDuongID;
                this.directDelete = false;
            }

            public ChildCriteria(int _KBDieuDuongID, bool directDelete)
            {
                this.KBDieuDuongID = _KBDieuDuongID;
                this.directDelete = directDelete;
            }

            public ChildCriteria() { }
        }
        #endregion Criteria
        //==========================================================================

        #region Create
        [RunLocal]
        private void DataPortal_Create(RootCriteria criteria)
        {
        }

        private void DataPortal_Create(ChildCriteria criteria)
        {
            MarkAsChild();
            ValidationRules.CheckRules();
        }

        #endregion Create
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        protected void DataPortal_Fetch(RootCriteria criteria)
        {
            this.Execute_Fetch(criteria);
        }

        protected void DataPortal_Fetch(string where)
        {
            this.Execute_Fetch(where);
        }

        protected void Execute_Fetch(RootCriteria criteria)
        {
            this.This_Fetch(criteria);
        }

        protected void Execute_Fetch(string where)
        {
            this.This_Fetch(where);
        }

        private void This_Fetch(RootCriteria criteria)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("KBDieuDuongReduceBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[KBDieuDuongID] = " + criteria.KBDieuDuongID);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void This_Fetch(string where)
        {
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("KBDieuDuongReduceBO_GetBO");
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
            using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
            {
                if (dataReader.Read())
                    Fetch(dataReader);
            }
        }

        private void Fetch(SqlDataReader dataReader)
        {
            double? TmpMach = null;
            double? TmpNhietDo = null;
            double? TmpHuyetAp1 = null;
            double? TmpHuyetAp2 = null;
            double? TmpNhipTho = null;
            double? TmpCanNang = null;
            double? TmpChieuCao = null;

            if (!(dataReader["KBDieuDuongID"] is DBNull))
                this._KBDieuDuongID = (int)dataReader["KBDieuDuongID"];
            if (!(dataReader["BenhNhanID"] is DBNull))
                this._BenhNhanID = (int)dataReader["BenhNhanID"];
            if (!(dataReader["Ngay"] is DBNull))
                this._Ngay = (DateTime)dataReader["Ngay"];
            if (!(dataReader["DieuDuongID"] is DBNull))
                this._DieuDuongID = (int)dataReader["DieuDuongID"];
            if (!(dataReader["ToanThan"] is DBNull))
                this._ToanThan = (string)dataReader["ToanThan"];
            if (!(dataReader["Mach"] is DBNull))
                TmpMach = (double)dataReader["Mach"];
            if (!(dataReader["NhietDo"] is DBNull))
                TmpNhietDo = (double)dataReader["NhietDo"];
            if (!(dataReader["HuyetAp1"] is DBNull))
                TmpHuyetAp1 = (double)dataReader["HuyetAp1"];
            if (!(dataReader["HuyetAp2"] is DBNull))
                TmpHuyetAp2 = (double)dataReader["HuyetAp2"];
            if (!(dataReader["NhipTho"] is DBNull))
                TmpNhipTho = (double)dataReader["NhipTho"];
            if (!(dataReader["CanNang"] is DBNull))
                TmpCanNang = (double)dataReader["CanNang"];
            if (!(dataReader["ChieuCao"] is DBNull))
                TmpChieuCao = (double)dataReader["ChieuCao"];

            if (TmpMach != null)
                this._Mach = TmpMach.Value.ToString() + " lần/ph";
            if (TmpNhietDo != null)
                this._NhietDo = TmpNhietDo.Value.ToString() + "°C";
            if (TmpHuyetAp1 != null)
                this._HuyetAp = TmpHuyetAp1.Value.ToString() + "/" + TmpHuyetAp2.Value.ToString() + " mm/Hg";
            if (TmpNhipTho != null)
                this._NhipTho = TmpNhipTho.Value.ToString() + " lần/ph";
            if (TmpCanNang != null)
                this._CanNang = TmpCanNang.Value.ToString() + " kg";
            if (TmpChieuCao != null)
                this._ChieuCao = TmpChieuCao.Value.ToString() + " cm";
        }
        #endregion Fetch
        //==========================================================================

        #region Insert
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Insert()
        {
            this.Execute_Insert();
        }

        protected void Execute_Insert()
        {
            this.This_Insert();
        }

        private void This_Insert()
        {
        }
        #endregion Insert
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            this.Execute_Update();
        }

        protected void Execute_Update()
        {
            this.This_Update();
        }

        private void This_Update()
        {
        }
        #endregion Update
        //==========================================================================

        #region Delete
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_DeleteSelf()
        {
            DataPortal_Delete(new RootCriteria(_KBDieuDuongID.Value));
        }

        private void DataPortal_Delete(RootCriteria criteria)
        {
            this.Execute_Delete(criteria);
        }

        protected void Execute_Delete(RootCriteria criteria)
        {
        }
        #endregion Delete
        //==========================================================================

        #region Internal method
        internal void Insert()
        {
            if (!this.IsDirty) return;
            DataPortal_Insert();
            MarkOld();
        }

        internal void Update()
        {
            if (!this.IsDirty) return;
            DataPortal_Update();
            MarkOld();
        }

        internal void DeleteSelf()
        {
            if (!this.IsDirty) return;
            if (this.IsNew) return;
            DataPortal_Delete(new RootCriteria(_KBDieuDuongID.Value));
            MarkNew();
        }
        #endregion Internal method
        //==========================================================================
    }
}
